package netty.io.demo.Netty.udp.unicast;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

/**
 * 应答端
 *
 * @author lihh
 */
public class UdpAnswerSide {
  
  public final static String ANSWER = "古诗来了：";
  
  public void run(int port) throws InterruptedException {
    NioEventLoopGroup group = new NioEventLoopGroup();
    
    try {
      /*和tcp的不同，udp没有接受连接的说法，所以即使是接收端，
            也使用Bootstrap*/
      Bootstrap b = new Bootstrap();
      b.group(group)
          .channel(NioDatagramChannel.class)
          .handler(new AnswerHandler());
      
      // 没有接受客户端连接的过程，监听本地端口即可
      ChannelFuture cf = b.bind(port).sync();
      System.out.println("应答服务 启动 ...");
      cf.channel().closeFuture().sync();
    } finally {
      // 这里 优雅关闭
      group.shutdownGracefully().sync();
    }
  }
  
  public static void main(String[] args) throws InterruptedException {
    new UdpAnswerSide().run(10030);
  }
}
